/*
 * Author: Jeff Liu(jebberwocky@gmail.com)
 * Date: Sep 5, 2010 7:42:07 PM
 * 
 * License: MIT
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package net.citiport.ggltranslate.impl;


import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import net.citiport.ggltranslate.api.DetectedLangParser;
import net.citiport.ggltranslate.ggl.DetectData;
import net.citiport.ggltranslate.ggl.DetectedLang;
import net.citiport.ggltranslate.util.DataUtil;

/**
 * The Class JsonDetectParser.
 * using json-simple @see code.google.com/p/json-simple
 */
public class JsonDetectParser implements DetectedLangParser {

	@Override
	public DetectedLang parse(String text) {
		if(text !=null && !text.isEmpty()){
			DetectedLang dl = new DetectedLang();
			DetectData dd = new DetectData();
			JSONObject object =  (JSONObject)JSONValue.parse(text);
			JSONObject rd = (JSONObject)object.get("responseData");
			dd.language = rd.get("language").toString();
			dd.isReliable = "true".equals(rd.get("isReliable").toString().trim());
			dd.confidence = (Double)rd.get("confidence");
			dl.responseData = dd;
			dl.responseDetails = DataUtil.ObjectToString(object.get("responseDetails"));
			dl.responseStatus = DataUtil.ObjectToString(object.get("responseStatus"));
			return dl;
		}else
			return null;
	}

}
